<?php

	class MyMailer	{
		private $From;
		private $To;
		private $Cc;
		private $Bcc;
		private $Subject;
		private $Message;
		private $HTMLMessage;
		private $ReplyTo;
		private $IsHTML;
		private $Boundry;
		private $attachments=array();
		private $attachmentmimes=array();
		private $attachmenttype=array();
		
		function __construct(){
			  // Set Boundry Line Breaker End Of Line //
			if (!defined('PHP_EOL')) define('PHP_EOL', strtoupper(substr(PHP_OS,0,3) == 'WIN') ? "\r\n" : "\n");
			$randtime = md5(time()); 
			$this->Boundry="0000_PHP5_0000"; //"==MissionSoft{$randtime}x";
		}

		/* All Properties Set Parameters */
		function From($FromAddress)
		{
			$this->From=$FromAddress;
		}

		function To($ToAddress)
		{
			$this->To=$ToAddress;
		}

		function Cc($CcAddress)
		{
			$this->Cc=$CcAddress;
		}

		function Bcc($BccAddress)
		{
			$this->Bcc=$BccAddress;
		}

		function Subject($Subject)
		{
			$this->Subject=$Subject;
		}

		function Message($Message, $IsHTML)
		{
			if(isset($IsHTML) && $IsHTML==true){
				$this->Message="";
				$this->HTMLMessage=$Message;
				$this->IsHTML=$IsHTML;
			}
			else{
				$this->Message=$Message;
				$this->HTMLMessage="";
				$this->IsHTML=$IsHTML;
			}
		}

		function ReplyTo($ReplyToAddress)
		{
			$this->ReplyTo=$ReplyToAddress;
		}


		function ClearAttachments()
		{
			$this->attachments=array();
			$this->attachmentmimes=array();
			$this->attachmenttype=array();
			
		}

		function AddAttachment($FileName,$MimeType="application/octet-stream",$Disposition="attachment")
		{
			$this->attachments[]=$FileName;
			$this->attachmentmimes[]=$MimeType;
			$this->attachmenttype[]=$Disposition;
		}

		function CreateFieldHeader()
		{
			$Header="";
			
			if(isset($this->From) && $this->From!="")
			{
				$from=$this->From;
				$Header.="From: $from ".PHP_EOL;
			}

			if(isset($this->ReplyTo) && $this->ReplyTo!="")
			{
				$ReplyTo=$this->ReplyTo;
				$Header.="Reply-To: $ReplyTo ".PHP_EOL;
			}

			if(isset($this->Cc) && $this->Cc!="")
			{
				$Cc=$this->Cc;
				$Header.="CC: $Cc ".PHP_EOL;
			}

			if(isset($this->Bcc) && $this->Bcc!="")
			{
				$Bcc=$this->Bcc;
				$Header.="BCC: $Bcc ".PHP_EOL;
			}
			return $Header;
		}


		function CreateHeader()
		{
			
			$Header="";
			if(isset($this->IsHTML) && $this->IsHTML==true){		///////////// HTML Message
				if(count($this->attachments)>0){	//////////// With Attachments /////////////////////
					$boundary=$this->Boundry;
					
					$NewHeader="";
					$NewHeader .="MIME-Version: 1.0".PHP_EOL;
					$NewHeader .="Content-Type: multipart/mixed; boundary=\"$boundary\";";
					$NewHeader .="--".$boundary.PHP_EOL.PHP_EOL;
					$NewHeader .=$this->CreateAttachments();
					$NewHeader .="MIME-Version: 1.0".PHP_EOL;
					$NewHeader .="Content-Type: text/html; charset=\"iso-8859-1\"".PHP_EOL;
					$NewHeader .="Content-Transfer-Encoding: 8bit".PHP_EOL;  
					$NewHeader .=$this->HTMLMessage.PHP_EOL;
					$Header .=$NewHeader;				
				}
				else{		//////////// Without Attachments /////////////////////
					$Header .= "MIME-Version: 1.0".PHP_EOL;
					$Header .= "Content-type: text/html; charset=iso-8859-1".PHP_EOL;
				}
			
			}
			else{	///////////// Plain Text Message
				if(count($this->attachments)>0)	{//////////// With Attachments /////////////////////
					$boundary=$this->Boundry;
					$NewHeader ="MIME-Version: 1.0".PHP_EOL;
					$NewHeader .="Content-Type: multipart/mixed; boundary=\"$boundary\";";
					$NewHeader .="--".$boundary.PHP_EOL;
					$NewHeader .="Content-Type: text/plain; charset=\"iso-8859-1\"".PHP_EOL;
					$NewHeader .="Content-Transfer-Encoding: 8bit".PHP_EOL.PHP_EOL;  
					$NewHeader .=$this->Message.PHP_EOL.PHP_EOL;
					$NewHeader .=$this->CreateAttachments();
					$Header.=$NewHeader;				
				}
			}
			return $Header;
		}
		
		function CreateAttachments()
		{
			$mime_boundary=$this->Boundry;
			
			$BigData="";
						
			if(count($this->attachments)>0){
				for($i=0;$i<count($this->attachments);$i++){
					$filePath=$this->attachments[$i];
					$FileName=basename($filePath);
	
					$file = fopen($filePath,'rb');
					$data = fread($file,filesize($filePath)); 
					fclose($file); 
					$MIMEType=$this->attachmentmimes[$i];
					$Disposition=$this->attachmenttype[$i];
					$SmallData ="--$mime_boundary".PHP_EOL;
                  	$SmallData .="Content-Type: {$MIMEType};".PHP_EOL;
                  	$SmallData .=" name=\"{$FileName}\"".PHP_EOL;
				    $SmallData .="Content-Transfer-Encoding: base64".PHP_EOL; 
					$SmallData .="Content-Disposition: $Disposition;".PHP_EOL; 
				    $SmallData .=" filename=\"{$FileName}\"".PHP_EOL.PHP_EOL; 
                 	$SmallData .=chunk_split(base64_encode($data)).PHP_EOL;
					$BigData.=$SmallData;
				}
				$BigData.="--".$mime_boundary.PHP_EOL;
			}
			
			return $BigData;
		}

		function SendMail()
		{
			
			$FromHeader=$this->CreateFieldHeader();
			$Header=$this->CreateHeader();

			if(isset($this->IsHTML)  && $this->IsHTML==true){
				if(count($this->attachments)>0){
					$result=mail($this->To,$this->Subject,$this->HTMLMessage,$FromHeader.$Header);	
				}
				else{
					$result=mail($this->To,$this->Subject,$this->HTMLMessage,$FromHeader.$Header);	
				}
			}
			else{
				if(count($this->attachments)>0){
					$result=mail($this->To,$this->Subject,$this->Message,$FromHeader.$Header);	
				}
				else{
					$result=mail($this->To,$this->Subject,$this->Message) or die();	
				}
			}
			
			return $result;
		}

}
?>
